import { createAction } from 'redux-actions'
import { check, body,delJson, getJson, postJson, parseJSON, checkStatus, data, putJson } from './util'
import { success } from '../ui-components'
import util from '../util'

export
const fetchTransformList = createAction('fetch transfer list', (arg, cb)=>
  fetch(`/employ-managements/job-transfers?${util.body(arg)}`, getJson)
		.then(checkStatus)
		.then(parseJSON)
		.then(check)
    .then(res => {
      cb && cb()
      return res.data
    })
)

export
const fetchTransferDetail = createAction('fetch transfer detail', (transferId, cb) =>
  fetch(`/employ-managements/job-transfers/${transferId}`, getJson)
		.then(checkStatus)
		.then(parseJSON)
		.then(check)
    .then(res => {
      cb && cb()
      return res.data
    })
)

export 
const fetchEmployManagement = createAction('fetch employ management', (arg, cb) =>
  fetch(`/employ-managements/status-updates?${util.body(arg)}`, getJson)
		.then(checkStatus)
		.then(parseJSON)
		.then(check)
    .then(res => {
      cb && cb()
      return res.data
    })
)

export
const updateEmployManagement = createAction('_', (arg, cb) =>
  fetch('/employ-managements/status-updates', { 
    ...postJson,
    ...body(arg)
  })
		.then(checkStatus)
		.then(parseJSON)
		.then(check)
    .then(cb)
)

export
const fetchEmployManagementDetail = createAction('fetch employ management detail', statusUpdateId =>
  fetch(`/employ-managements/status-updates/${statusUpdateId}`, getJson)
		.then(checkStatus)
		.then(parseJSON)
		.then(check)
    .then(data)
)

export
const resetEmployManagement = createAction('reset employ management')

export
const transferUser = createAction('_', (arg, cb) =>
  fetch('/employ-managements/job-transfers', { 
    ...postJson,
    ...body(arg)
  })
		.then(checkStatus)
		.then(parseJSON)
		.then(check)
    .then(cb)
)

export
const resetTransfer = createAction('reset transfer')

export
const revokeUser = createAction('_', (statusUpdateId, statusType, cb) =>
  fetch(`/employ-managements/status-updates/${statusUpdateId}/revoke`, {
    ...putJson,
    ...body({ statusType })
  })
		.then(checkStatus)
		.then(parseJSON)
		.then(check)
		.then(cb)
)
